<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('销售订单列表')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list" id="userWorker">
                        <ul>
                            <li>
                                <label class="text-center" >水电师傅：</label>
                            </li>
                            <li>
                                <select name="workerId" class="workerChoose form-control" onchange="workerChange()"><option value="">全部</option></select>
                            </li>
                            <li>
                                <label class="text-center">客户名称：</label>
                            </li>
                            <li>
                                <select name="consumerId" class="userChoose form-control" onchange="$.table.refresh()"><option value="">全部</option></select>
                            </li>
                            <li>
                                <button type="button" class="btn btn-dark" onclick="userAdd()"><i class="fa fa-plus"> 新增客户</i></button>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="orderStatus" class="form-control" th:with="type=${@dict.getType('order_status')}" onchange="$.table.refresh()">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="location.reload()"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <label>&nbsp;&nbsp;&nbsp;</label>
                <a class="btn btn-success" onclick="createOrder()" >
                    <h3><i class="fa fa-plus"></i> 新开订单</h3>
                </a>
                <label>&nbsp;&nbsp;&nbsp;</label>
                <a class="btn btn-info" onclick="createOrderV2()" >
                    <h3><i class="fa fa-plus"></i> 新开单</h3>
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <script th:inline="javascript">

        var orderStatusDatas = [[${@dict.getType('order_status')}]];
        var orderTypeDatas = [[${@dict.getType('order_type')}]];

        $(function() {
            var options = {
                url: ctx + "order/manage/list",
                removeUrl: ctx + "order/manage/remove",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,

                modalName: "销售订单",
                columns: [{
                    radio: true
                },
                {
                    field: 'orderId',
                    align: 'center',
                    width: '10',
                    widthUnit: '%',
                    title: '订单号'
                },
                {
                    field: 'workerName',
                    align: 'center',
                    title: '水电师傅'
                },
                {
                    field: 'consumerName',
                    align: 'center',
                    title: '客户'
                },
                {
                    field: 'totalMoney',
                    align: 'center',
                    title: '总价'
                },
                {
                    field: 'orderStatus',
                    title: '状态',
                    align: 'center',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(orderStatusDatas, value);
                    }
                },
                {
                    field: 'orderType',
                    title: '退货单',
                    align: 'center',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(orderTypeDatas, value);
                    }
                },
                {
                    field: 'remark',
                    align: 'center',
                    title: '备注'
                },
                {
                    field: 'createTime',
                    title: '开单日期',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var date = new Date(value);
                        var day = date.getDate();
                        day = day < 10 ? '0' + day : day;
                        var month = date.getMonth();
                        month += 1;  // JavaScript months are 0-11
                        month = month < 10 ? '0' + month : month;
                        var year = date.getFullYear();
                        return year + '-' + month + '-' + day
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.orderStatus == 0){
                            actions.push('<a class="btn btn-success btn-xs ' + '" href="javascript:void(0)" onclick="editOrder(\'' + row.orderId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.orderId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }
                        else {
                            actions.push('<a class="btn btn-primary btn-xs ' + '" href="javascript:void(0)" onclick="viewOrder(\'' + row.orderId + '\')"><i class="fa fa-television"></i>查看</a> ');
                            if(row.orderStatus == 1){
                                if(row.orderType == 1){
                                    actions.push('<a class="btn btn-warning btn-xs ' + '" href="javascript:void(0)" onclick="payOrder(\'' + row.orderId + '\')"><i class="fa fa-credit-card"></i>暂付</a> ');
                                    actions.push('<a class="btn btn-info btn-xs ' + '" href="javascript:void(0)" onclick="payReturn(\'' + row.orderId + '\')"><i class="fa fa-television"></i>结算</a> ');
                                    // actions.push('<a class="btn btn-warning btn-xs ' + '" href="javascript:void(0)" onclick="payReturn(\'' + row.orderId + '\')"><i class="fa fa-credit-card"></i>付款 结算</a> ');
                                }
                                if(row.orderType == 3){
                                    actions.push('<a class="btn btn-info btn-xs ' + '" href="javascript:void(0)" onclick="closeReturn(\'' + row.orderId + '\')"><i class="fa fa-credit-card"></i>结算</a> ');
                                }
                                // actions.push('<a class="btn btn-warning btn-xs ' + '" href="javascript:void(0)" onclick="payOrder(\'' + row.orderId + '\')"><i class="fa fa-credit-card"></i>暂付</a> ');
                                // actions.push('<a class="btn btn-success btn-xs ' + '" href="javascript:void(0)" onclick="returnOrder(\'' + row.orderId + '\')"><i class="fa fa-television"></i>退货</a> ');
                                actions.push('<a class="btn btn-danger btn-xs ' + '" href="javascript:void(0)" onclick="deleteOrder(\'' + row.orderId + '\')"><i class="fa fa-remove"></i> 作废</a>');
                            }
                        }
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        //拉取师傅信息
        $(function () {
            $.ajax({
                type: "post",
                url: ctx + "hh/user/selectList",
                data: {
                  "roleId": 101
                },
                success: function(result) {
                    if (result.code == 0) {
                        $('#userWorker').cxSelect({
                            required: false,
                            firstTitle: '全部',
                            selects: ['workerChoose'],
                            jsonName: 'name',
                            jsonValue: 'id',
                            data: result.data
                        });
                    }
                }
            })
        });

        //拉取客户信息
        function workerChange() {
            $.ajax({
                type: "post",
                url: ctx + "hh/user/selectList",
                data: {
                    "workerId": $(".workerChoose").val()
                },
                success: function(result) {
                    if (result.code == 0) {
                        $('#userWorker').cxSelect({
                            required: false,
                            firstTitle: '全部',
                            selects: ['userChoose'],
                            jsonName: 'name',
                            jsonValue: 'id',
                            data: result.data
                        });
                        $.table.refresh();
                    }
                }
            })
        }

        //新开订单
        function createOrder() {
            var index = layer.open({
                type: 2,
                area: ['800px', '700px'],
                shade: 0.3,
                title: '添加商品',
                content: ctx + "order/manage/add",
                btn: [],
                end: function () {
                    $.table.refresh();
                }
            });
            layer.full(index);
        }

        //新开单
        function createOrderV2() {
            var orderFlag = true;
            var userId = $(".userChoose").val();
            if (userId == null || userId.length == 0) {
                $.modal.alertError("先选择客户，再开单！");
                return false;
            }

            $.ajax({
                type: "post",
                url: ctx + "order/manage/createCheck",
                data: {
                    "workerId": $(".workerChoose").val(),
                    "consumerId": $(".userChoose").val()
                },
                success: function (result) {
                    if (result.code == 0) {
                        if (0 != result.data) {
                            orderFlag = false;
                            $.modal.confirm("客户【" + $(".userChoose").find("option:selected").text() + "】有未打印订单，是否继续新开单?", function () {
                                createSaleOrder();
                            });
                        }
                        if (orderFlag) {
                            createSaleOrder();
                        }
                    }
                }
            })
        }

        function createSaleOrder() {
            $.ajax({
                type: "post",
                url: ctx + "order/manage/create",
                data: {
                    "workerId": $(".workerChoose").val(),
                    "consumerId": $(".userChoose").val()
                },
                success: function (result) {
                    if (result.code == 0) {
                        editOrder(result.data.orderId);
                    }
                }
            })
        }

        //编辑订单
        function editOrder(orderId) {
            var index = layer.open({
                type: 2,
                area: ['800px', '700px'],
                shade: 0.3,
                title: '修改订单',
                content: ctx + "order/manage/edit/" + orderId,
                btn: [],
                end: function () {
                    $.table.refresh();
                }
            });
            layer.full(index);
        }

        //查看订单
        function viewOrder(orderId) {
            var index = layer.open({
                type: 2,
                area: ['800px', '700px'],
                shade: 0.3,
                title: '查看订单',
                content: ctx + "order/manage/view/" + orderId,
                btn: [],
                end: function () {
                    $.table.refresh();
                }
            });
            layer.full(index);
        }

        //客户付款
        function payOrder(orderId) {
            layer.open({
                type: 2,
                area: ['500px', '280px'],
                shade: 0.3,
                title: '暂付款',
                content: ctx + "order/manage/pay/" + orderId,
                btn: [],
                end: function () {
                    $.table.refresh();
                }
            });
        }

        //付款退货
        function payReturn(orderId) {
            layer.open({
                type: 2,
                area: ['600px', '270px'],
                shade: 0.3,
                title: '退货结算',
                content: ctx + "order/manage/return/" + orderId,
                btn: [],
                end: function () {
                    $.table.refresh();
                }
            });
        }

        //付款退货
        function closeReturn(orderId) {
            $.modal.confirm("确认结算吗?", function () {
                $.ajax({
                    type: "post",
                    url: ctx + "close/manage/closeReturn",
                    data: {
                        "orderId": orderId,
                        "payType": '2',
                        "payAmount": '0'
                    },
                    success: function(result) {
                        if (result.code == 0) {
                            $.table.refresh();
                            layer.alert("结算成功", {
                                icon: $.modal.icon(modal_status.SUCCESS),
                                title: "系统提示",
                                btn: ['确认'],
                                btnclass: ['btn btn-primary'],
                                end: function () {
                                    $.modal.openTab("结算管理", "/close/manage")
                                }
                            });
                        }
                    }
                })
            });
        }

        //作废订单
        function deleteOrder(orderId) {
            $.modal.confirm("确认要作废订单：" + orderId + " 吗?", function() {
                $.ajax({
                    type: "post",
                    url: ctx + "order/manage/delete",
                    data: {
                        "orderId": orderId
                    },
                    success: function(result) {
                        if (result.code == 0) {
                            $.table.refresh();
                        }
                    }
                })
            });
        }

        /* 新增客户 */
        function userAdd() {
            layer.open({
                type: 2,
                area: ['600px', '500px'],
                fix: false,
                shade: 0.3,
                title: '新增客户',
                content: ctx + "hh/user/userAdd",
                btn: [],
                end: workerChange
            });
        }
    </script>
</body>
</html>